package pages;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import corp.ControlAccount;
import corp.Corporation;
import corp.ServiceResponse;

import merchant.dao.Merchant;

import utils.CorporateDAO;
import utils.FourSquareDAO;
import utils.MerchantDAO;
import utils.TwitterDAO;

/**
 * Servlet implementation class Index
 */
@WebServlet("/Index")
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Index() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("Hello");
		TwitterDAO d = TwitterDAO.fetchDAO();
		try {
			List<String> tweets = d.fetchTweets("hackathon1212");
			Iterator it = tweets.iterator();
			while (it.hasNext()) {
				String t = (String) it.next();
				out.println(t);
				out.println("<BR><BR>");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		out.println("<BR><BR>");
		MerchantDAO mDAO = new MerchantDAO("http://hackathon1212.appspot.com");
		
		Merchant m = mDAO.getMerchant("5211111111");
		
		if (m != null) {
			out.println(m.getCity());
		} else {
			out.println("Could not find merchant");
		}
		
		CorporateDAO cDAO = new CorporateDAO("http://hackathon1212.appspot.com");
		out.println("TEST");
		ServiceResponse<Corporation> sr = cDAO.getCorporation("000009");
		String s = (sr.getO().toString());
		out.println(s);
	 //	Corporation c = new Gson().fromJson(s, Corporation.class);
		
		 Corporation c = sr.getO();
		out.println(c.getCorporationName());
		
	//	ServiceResponse<ControlAccount> sr2 = cDAO.addCtrlAccount("000009","0012");
	//	ControlAccount ca = sr2.getO();
	//	out.println(ca.getControlAccountName());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
